// pages/MoreImage/MoreImage.js
const app = getApp()
var util = require("../../utils/utils.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hidden: true,
    imageList: [],
    scrollTop: 0,
    scrollHeight: 0,
    isOver: false,
    isCollect: false, // 默认没有收藏
    urls: [],
    loaymore: [],
    pageindex: 1,
    callbackcount: 15,
    loadSongList: [], //放置返回数据的数组
    isFromSearch: true, // 用于判断loadSongList数组是不是空数组，默认true，空的数组
    loadLoading: false, //"上拉加载"的变量，默认false，隐藏
    loadLoadingComplete: false, //“没有数据”的变量，默认false，隐藏
    loadText: "加载更多",
    page: 1,
    rows: 15,
    successSearchLoading: false,
    successSearchLoadingComplete: false,
    pendingSearchLoading: false,
    pendingSearchLoadingComplete: false,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getUserInfo(options);
  },
  initData: function(options) {
    let marineId = options.marineId;
    if(marineId == undefined){
      marineId =null
    }
    this.setData({
      marineId: marineId,
      activityId: options.activityId
    })
    this.queryActResourceList();
    this.queryActInfo(options);
  },
  queryActInfo: function(options) {
    let that = this
    // 视频
    wx.request({
      url: app.httpUrl + '/activity/queryActInfo/' + options.activityId,
      header: {
        'xcx-xtoken': app.globalData.openid,
        'content-type': 'application/json' // 默认值
      },
      method: "GET",
      success: (res) => {
        var activity = res.data.data;
        that.setData({
          activity: activity
        })
      }
    })
  },
  lower: function(e) {
    if (!this.data.isOver) {
      this.queryActResourceList();
    }
  },
  queryActResourceList: function() {
    let that = this;
    let marineId = that.data.marineId;
    if (!marineId){
      marineId = null
    }
    wx.request({
      // url: app.httpUrl + '/activity/live/queryListByPage',
      url: app.httpUrl + '/activity/live/queryListForImgByPage',
      data: {
        marineId: that.data.marineId,
        activityId: that.data.activityId,
        page: this.data.page,
        rows: this.data.rows,
        msgType: "image",
      },
      header: {
        'xcx-xtoken': app.globalData.openid,
        'content-type': 'application/json' // 默认值
      },
      method: 'POST',
      success: function(res) {
        if (that.data.page >= res.data.data.totalPage) {
          that.setData({
            isOver: true,
            successSearchLoadingComplete: true
          })
        }
        let searchList = []; //空数组放数据
        that.data.page += 1; //请求完后页数+1
        that.data.results ? searchList = data.data.results : searchList = that.data.imageList.concat(res.data.data.results) //判断data里面的数组是否为空 
        let arr = [] //新照片数组
        for (let a = 0; a < searchList.length; a++) {
          searchList[a].createTime = util.formatDate(searchList[a].createTime, 'Y年M月D日');
        }
        // for (let a = 0; a < list.length; a++) {
        //   for (let b = 0; b < list[a].attachList.length; b++) {
        //     list[a].attachList[b].createTime = util.formatDate(list[a].attachList[b].createTime, 'Y年M月D日');
        //     list[a].attachList[b].updateTime = util.formatDate(list[a].attachList[b].updateTime, 'Y年M月D日');
        //   }
        // }
        that.setData({
          imageList: searchList,
          page: that.data.page
        });
      }
    })
  },
  addActivityZan: function(event) {
    var liveid = event.currentTarget.dataset.liveid;
    var idx = event.currentTarget.dataset.idx;
    var that = this;
    wx.request({
      url: app.httpUrl + '/activity/live/addActivityZan',
      data: {
        liveId: liveid
      },
      header: {
        'xcx-xtoken': app.globalData.openid,
        'content-type': 'application/json' // 默认值
      },
      method: 'POST',
      success: function(res) {
        if (res.data.code == '000') {
          var list = that.data.imageList;
          list[idx].zanId = res.data.data.id;
          list[idx].zanCount = list[idx].zanCount + 1;
          that.setData({
            imageList: list
          })
        }
      }
    })
  },
  addClicks: function(id) {
    wx.request({
      url: app.httpUrl + '/activity/live/addClicks',
      header: {
        'xcx-xtoken': app.globalData.openid,
        'content-type': 'application/json' // 默认值
      },
      method: "POST",
      data: {
        id: id
      },
      success: (res) => {}
    })
  },
  // 加载更多
  loadMore: function(e) {
    let index = e.currentTarget.dataset.index;
    this.queryActResourceList(index);
  },
  // 图片预览
  previewImage: function(e) {
    let that = this;
    var src = e.currentTarget.dataset.src; //获取data-src
    let arr = that.data.imageList
    let newimage = []; //获取图片数组
    for (let a = 0; a < arr.length; a++) {
      newimage.push('http://img.xue110.top/taoxuequ' + arr[a].sysUrl)
    }
    wx.previewImage({
      current: src, // 当前显示图片的http链接 
      urls: newimage // 需要预览的图片http链接列表     
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    return {
      title: '@快乐 分享我的精彩瞬间 From陶学趣',
      path: '/pages/MoreImage/MoreImage?activityId=' + this.data.activityId + '&marineId=' + this.data.marineId,
      imageUrl: app.imgUrl + this.data.activity.activityImgUrl, //这是我的图片路径
      success: function(res) {
        //转发成功
      }
    }
  },
  searchScroll: function() {
    let that = this;
    if (that.data.currentData == 0) {
      if (!that.data.successSearchLoading && !that.data.successSearchLoadingComplete) {
        that.setData({
          successSearchLoading: true,
          successSearchLoadingComplete: true
        })
        that.fitchSearch();
      }
    } else if (that.data.currentData == 1) {
      if (!that.data.pendingSearchLoading && !that.data.pendingSearchLoadingComplete) {
        that.setData({
          pendingSearchLoading: true,
          pendingSearchLoadingComplete: true
        })
        that.fitchSearch();
      }
    } else if (that.data.currentData == 2) {
      if (!that.data.allSearchLoading && !that.data.allSearchLoadingComplete) {
        that.setData({
          allSearchLoading: true,
          allSearchLoadingComplete: true
        })
        that.fitchSearch();
      }
    }
  },
  getUserInfo: function(options) {
    let that = this;
    if (app.globalData.appUser != null) {
      this.initData(options);
      return;
    }
    wx.login({
      success: function(res) {
        if (res.code) {
          //发起网络请求
          wx.request({
            url: app.httpUrl + '/common/getUserInfo',
            data: {
              code: res.code
            },
            header: {
              'content-type': 'application/json' // 默认值
            },
            method: "POST",
            success: function(res) {
              if (res.data.code == '000') {
                app.globalData.appUser = res.data.data;
                app.globalData.openid = res.data.data.openid;
                that.initData(options);
              }
            }
          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    });
  }
})